package com.lb.shopguide.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lb.baselib.base.BaseFragment;
import com.lb.shopguide.R;
import com.lb.shopguide.entity.recommend.MaterialGoodsBean;
import com.lb.shopguide.ui.view.GridListItemDecoration;

import java.util.List;

/**
 * Create by kangzhao on 2018/11/23
 */
public class AdapterMaterialGoods extends BaseQuickAdapter<MaterialGoodsBean, BaseViewHolder> {
    private BaseFragment fragment;

    public AdapterMaterialGoods(int layoutResId, @Nullable List<MaterialGoodsBean> data, BaseFragment fragment) {
        super(layoutResId, data);
        this.fragment = fragment;
    }

    @Override
    protected void convert(BaseViewHolder helper, MaterialGoodsBean item) {
        View view = helper.itemView;

        View ivDivider = view.findViewById(R.id.iv_divider);
        TextView tvGoodsDesc = view.findViewById(R.id.tv_goods_desc);
        TextView tvGoodsPrice = view.findViewById(R.id.tv_goods_price);
        TextView tvEarnPrice = view.findViewById(R.id.tv_earn_price);

        LinearLayout layoutShareLayout = view.findViewById(R.id.layout_share);
        RecyclerView rvGallery = view.findViewById(R.id.rv_image);
        LinearLayout layoutEarnMoney = view.findViewById(R.id.layout_earn_money);
        tvGoodsDesc.setText(item.getMatText());
//        if (item.isSecSymbol()) {
//            layoutEarnMoney.setVisibility(View.GONE);
//        } else {
//            layoutEarnMoney.setVisibility(View.VISIBLE);
//        }
        tvEarnPrice.setText("￥" + item.getStaffBen());
        tvGoodsPrice.setText("￥" + item.getSalePrice());

        rvGallery.setLayoutManager(new GridLayoutManager(mContext, 3));

        if (rvGallery.getItemDecorationCount() == 0) {
            rvGallery.addItemDecoration(new GridListItemDecoration(10, 3, 0));
        }
        AdapterGoodsGallery adapterGoodsGallery = new AdapterGoodsGallery(R.layout.item_goods_gallery, item.getSharePicUrls());
        rvGallery.setAdapter(adapterGoodsGallery);
        rvGallery.setHasFixedSize(true);
        rvGallery.setNestedScrollingEnabled(false);
        layoutShareLayout.setOnClickListener(v -> {
            if (mOnMaterialListener != null) {
                mOnMaterialListener.onShareCircle(item);
            }
        });
        adapterGoodsGallery.setOnItemClickListener((adapter, view1, position) -> {
            if (mOnMaterialListener != null) {
                mOnMaterialListener.viewImage(position, item.getSharePicUrls());
            }
        });
        if (mData.indexOf(item) == mData.size() - 1) {
            ivDivider.setVisibility(View.GONE);
        } else {
            ivDivider.setVisibility(View.VISIBLE);
        }
    }


    public interface OnMaterialListener {
        void onShareCircle(MaterialGoodsBean bean);

        void viewImage(int position, List<String> urls);
    }

    public void setOnMaterialListener(OnMaterialListener listener) {
        mOnMaterialListener = listener;
    }

    private OnMaterialListener mOnMaterialListener;
}
